package com.google.code.portlet;

import java.io.IOException;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletConfig;
import javax.portlet.PortletContext;
import javax.portlet.PortletException;
import javax.portlet.PortletPreferences;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

public class GooglePortlet extends GenericPortlet
{
	public void init(PortletConfig config) throws PortletException
	{
		super.init(config);
	}
	
	public void doView(RenderRequest request, RenderResponse response)
		throws PortletException, IOException
	{
        response.setContentType("text/html");
        PortletContext context = getPortletContext();
        PortletRequestDispatcher prd = context.getRequestDispatcher("/WEB-INF/jsp/google.jsp");
        
        PortletPreferences prefs = request.getPreferences();
        String key = prefs.getValue("KEY", "notset");
        request.setAttribute("KEY",key);
        
        prd.include(request, response);
	}

	public void doEdit(RenderRequest request, RenderResponse response)
		throws PortletException, IOException
	{
        response.setContentType("text/html");
        PortletContext context = getPortletContext();
        PortletRequestDispatcher prd = context.getRequestDispatcher("/WEB-INF/jsp/edit.jsp");

        PortletPreferences prefs = request.getPreferences();
        String key = prefs.getValue("KEY", "notset");
        request.setAttribute("KEY",key);
        
        prd.include(request, response);
	}

    public void processAction(ActionRequest request, ActionResponse response)
		throws PortletException, IOException
    {
        String key = request.getParameter("KEY");
        String action = request.getParameter("ACTION");

        System.out.println("key: " + key);
        System.out.println("action: " + action);

        if ("Save".equals(action)) {
            PortletPreferences prefs = request.getPreferences();
            prefs.setValue("KEY",key);
            prefs.store();
        }
    }
}

